package com.william.xiaobujiantest.Tools.LocationTool;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;

/**
 * Created by william on 2017/5/11.
 */

public class LocationTool {

    private static LocationManager locationManager;
    private static Location location;

    public static Location getLocation(Context context) throws LocationPermissionDeniedException,
        GPSNotInWorkException, InternetNotInWorkException {

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //1.通过GPS定位，较精确，也比较耗电
        String gpsProvider = "";
        //2.通过网络定位，对定位精度度不高或省电情况可考虑使用
        String netProvider = "";

        //检测手机是否开启gps功能
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            gpsProvider = LocationManager.GPS_PROVIDER;
            getLoca(context,gpsProvider);
        } else {
            throw new GPSNotInWorkException();
        }

        //当gps不可用时，检测是否开启了网络
        if (location == null && locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            netProvider = LocationManager.NETWORK_PROVIDER;
            getLoca(context,netProvider);
        } else {
            //todo 这里不应该这么写
//            throw new InternetNotInWorkException();
        }

        if (gpsProvider == null && netProvider == null) {

            //无法定位：1、提示用户打开定位服务；2、跳转到设置界面
            Toast.makeText(context, "无法定位，请打开GPS定位服务", Toast.LENGTH_SHORT).show();
//            Intent i = new Intent();
//            i.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
////            startActivity(i);
        }

        return location;
    }

    private static void getLoca(Context context, String locationProvider) throws LocationPermissionDeniedException {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            LocationListener listener = new LocationListener() {
                @Override
                public void onLocationChanged(Location loca) {
                    // 取到经纬度
                    location = loca;
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            };
            location = locationManager.getLastKnownLocation(locationProvider);
            if(location == null){
                //周期性更新
//                locationManager.requestLocationUpdates(locationProvider, 1000, 1, listener);
                //单次更新
                locationManager.requestSingleUpdate(locationProvider,listener,null);
            }

        }else{
            throw new LocationPermissionDeniedException();
        }
    }
}